// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.asm;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;

/**
 * Detects instructions.
 * 
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public class InsnAdviceAdapter extends MethodAdapter {

  /**
   * Initializes this adapter.
   * 
   * @param out output visitor.
   */
  public InsnAdviceAdapter(MethodVisitor out) {    
    super(out);
  }

  @Override
  public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    beforeInsn();
    super.visitFieldInsn(opcode, owner, name, desc);
    afterInsn();
  }

  @Override
  public void visitIincInsn(int var, int increment) {
    beforeInsn();
    super.visitIincInsn(var, increment);
    afterInsn();
  }

  @Override
  public void visitInsn(int opcode) {
    beforeInsn();
    super.visitInsn(opcode);
    afterInsn();
  }

  @Override
  public void visitIntInsn(int opcode, int operand) {
    beforeInsn();
    super.visitIntInsn(opcode, operand);
    afterInsn();
  }

  @Override
  public void visitJumpInsn(int opcode, Label label) {
    beforeInsn();
    super.visitJumpInsn(opcode, label);
    afterInsn();
  }

  @Override
  public void visitLdcInsn(Object cst) {
    beforeInsn();
    super.visitLdcInsn(cst);
    afterInsn();
  }

  @Override
  public void visitLineNumber(int line, Label start) {
    beforeInsn();
    super.visitLineNumber(line, start);
    afterInsn();
  }

  @Override
  public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
    beforeInsn();
    super.visitLookupSwitchInsn(dflt, keys, labels);
    afterInsn();
  }

  @Override
  public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    beforeInsn();
    super.visitMethodInsn(opcode, owner, name, desc);
    afterInsn();
  }

  @Override
  public void visitMultiANewArrayInsn(String desc, int dims) {
    beforeInsn();
    super.visitMultiANewArrayInsn(desc, dims);
    afterInsn();
  }

  @Override
  public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
    beforeInsn();
    super.visitTableSwitchInsn(min, max, dflt, labels);
    afterInsn();
  }

  @Override
  public void visitTypeInsn(int opcode, String type) {
    beforeInsn();
    super.visitTypeInsn(opcode, type);
    afterInsn();
  }

  @Override
  public void visitVarInsn(int opcode, int var) {
    beforeInsn();
    super.visitVarInsn(opcode, var);
    afterInsn();
  }
  
  /**
   * Called before every visited instruction.
   */
  protected void beforeInsn() {
    // Do nothing.
  }
  
  /**
   * Called after every visited instruction.
   */
  protected void afterInsn() {
    // Do nothing.
  }
}
